<?php
namespace seccion;


class secciones {
    protected $id;
    protected $name;
    protected $path;
    protected $tableName;

    function __construct($id=0)
    {
        global $config;

        $this->id = 0;
        $this->name = "";
        $this->path = "";
        $this->tableName = "`".$config["dbName"]."`.`secciones`";

        if ($id!=0){
            $this->loadSeccion($id);
        }
    }

    public function loadSeccion($id){
        global $db;

        $sSql = "select * from ".$this->tableName." where id = ". $id;
        $res = $db->execSql($sSql);

        if ($res){
            while ($row = mysqli_fetch_assoc($res)) {
                $this->setId($row["id"]);
                $this->setName($row["nombre"]);
                $this->setPath($row["path"]);
            }
        }
    }

    public function saveSeccion(){
        global $db;

        $rtrn = 0;
        $sSql = "select id from ".$this->tableName." where `nombre` = '". $this->name ."'";
        $res = $db->execSql($sSql);
        if (mysqli_num_rows($res) == 0){
            $sSql = "insert into ".$this->tableName." (`nombre`,`path`) values ('".$this->name."','".$this->path."')";

            if ($db->execSql($sSql)){
                $rtrn = $db->getInsertedId();
            }
        }else{
            $rtrn = mysqli_fetch_assoc($res)["id"];
        }

        return $rtrn;

    }

    /**
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param int $id
     */
    public function setId($id)
    {
        $this->id = $id;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param string $path
     */
    public function setPath($path)
    {
        $this->path = $path;
    }

    /**
     * @return string
     */
    public function getPath()
    {
        return $this->path;
    }

    /**
     * @return string
     */
    public function getTableName()
    {
        return $this->tableName;
    }



}